/*    */ package com.zimbra.cs.filter.jsieve;
/*    */ 
/*    */ import com.zimbra.common.mime.InternetAddress;
/*    */ import com.zimbra.common.util.Log;
/*    */ import com.zimbra.common.util.ZimbraLog;
/*    */ import com.zimbra.cs.filter.ZimbraMailAdapter;
/*    */ import com.zimbra.cs.mailbox.Mailbox;
/*    */ import com.zimbra.cs.mailbox.MailboxIndex;
/*    */ import java.io.IOException;
/*    */ import java.util.ArrayList;
/*    */ import java.util.Iterator;
/*    */ import java.util.List;
/*    */ import org.apache.jsieve.Argument;
/*    */ import org.apache.jsieve.Arguments;
/*    */ import org.apache.jsieve.ScriptCoordinate;
/*    */ import org.apache.jsieve.SieveContext;
/*    */ import org.apache.jsieve.StringListArgument;
/*    */ import org.apache.jsieve.TagArgument;
/*    */ import org.apache.jsieve.exception.SieveException;
/*    */ import org.apache.jsieve.mail.MailAdapter;
/*    */ import org.apache.jsieve.tests.AbstractTest;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public final class AddressBookTest
/*    */   extends AbstractTest
/*    */ {
/*    */   private static final String IN = ":in";
/*    */   private List<String> headers;
/*    */   
/*    */   protected void validateArguments(Arguments args, SieveContext ctx)
/*    */     throws SieveException
/*    */   {
/* 49 */     Iterator<Argument> itr = args.getArgumentList().iterator();
/* 50 */     if (itr.hasNext()) {
/* 51 */       Argument arg = (Argument)itr.next();
/* 52 */       if ((arg instanceof TagArgument)) {
/* 53 */         TagArgument tag = (TagArgument)arg;
/* 54 */         if (tag.is(":in")) {
/* 55 */           if (itr.hasNext()) {
/* 56 */             arg = (Argument)itr.next();
/* 57 */             if ((arg instanceof StringListArgument)) {
/* 58 */               this.headers = ((StringListArgument)arg).getList();
/*    */             } else {
/* 60 */               throw ctx.getCoordinate().syntaxException(":in is missing an argument");
/*    */             }
/*    */           } else {
/* 63 */             throw ctx.getCoordinate().syntaxException(":in is missing an argument");
/*    */           }
/*    */         } else {
/* 66 */           throw ctx.getCoordinate().syntaxException("Unknown tag: " + tag.getTag());
/*    */         }
/*    */       } else {
/* 69 */         throw ctx.getCoordinate().syntaxException("Unexpected argument: " + arg.getValue());
/*    */       }
/*    */     }
/*    */   }
/*    */   
/*    */   protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException
/*    */   {
/* 76 */     assert (this.headers != null);
/* 77 */     if (!(mail instanceof ZimbraMailAdapter)) {
/* 78 */       return false;
/*    */     }
/* 80 */     Mailbox mbox = ((ZimbraMailAdapter)mail).getMailbox();
/* 81 */     List<InternetAddress> addrs = new ArrayList();
/* 82 */     for (String header : this.headers) {
/* 83 */       for (String value : mail.getHeader(header)) {
/* 84 */         addrs.add(new InternetAddress(value));
/*    */       }
/*    */     }
/*    */     try {
/* 88 */       return mbox.index.existsInContacts(addrs);
/*    */     } catch (IOException e) {
/* 90 */       ZimbraLog.filter.error("Failed to lookup contacts", e);
/*    */     }
/* 92 */     return false;
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/filter/jsieve/AddressBookTest.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */